from django.shortcuts import render

# Create your views here.
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ViewSetMixin
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import APIException

from custom import models
from custom.serializer import CustomModelSerializer, CustomCommentViewSerializer, CustomCommentCreateSerializer, \
    PublishModelSerializer, MyPushSerializer

from utils.common_response import APIResponse

from utils.new_common_response import common_data
from utils.private import filters, pagination
from libs.send_tengxun import tengxun


class CustomView(ListModelMixin, GenericViewSet):
    """
    ymq
    用户案例
    """
    queryset = models.CustomProject.objects.filter(is_delete=False, is_show=True).all().order_by('-id')
    serializer_class = CustomModelSerializer

    filter_backends = [filters.MaxFilterBackend, filters.MinFilterBackend]
    pagination_class = pagination.DataLimitPagination

    def list(self, request, *args, **kwargs):
        """
        重写list
        """
        response = super().list(request, *args, **kwargs)
        old_data = response.data

        new_data = common_data(old_data)
        response.data = new_data

        return response


class CustomCommentView(RetrieveModelMixin, GenericViewSet):
    """

    用户案例详情页评论
    """
    queryset = models.CustomProject.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = CustomCommentViewSerializer

    def retrieve(self, request, *args, **kwargs):
        response = super().retrieve(request, *args, **kwargs)
        old_data = response.data
        new_data = common_data(old_data)
        response.data = new_data

        return response


class CustomCommenCreatetView(CreateModelMixin, GenericViewSet):
    """
    创建用户评论视图
    """
    serializer_class = CustomCommentCreateSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def create(self, request, *args, **kwargs):
        response = super().create(request, *args, **kwargs)
        old_data = response.data
        # print(old_data, '>>>>>>>>>>>>')

        new_data = common_data(old_data)
        response.data = new_data

        return response


class SendTengXunView(CreateModelMixin, GenericViewSet):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]
    serializer_class = PublishModelSerializer

    @action(methods=["GET"], detail=False)
    def send_miyao(self, request, *args, **kwargs):
        result_dict = tengxun.credential(bucket="youzhu-1318016344", region="ap-hongkong")
        return APIResponse(date=result_dict)

    @action(methods=["POST"], detail=False)
    def publish(self, request, *args, **kwargs):
        """
        发表
        """
        # print('???????????')
        serializer = self.get_serializer(data=request.data, context={"request": request})
        serializer.is_valid(raise_exception=True)
        return APIResponse(code=100, msg='发表成功')


class MyPushGenericViewSet(ListModelMixin, GenericViewSet):
    """
    我的发布
    """
    queryset = models.CustomProject.objects.filter(is_delete=False, is_show=True)
    serializer_class = MyPushSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        queryset = self.queryset.filter(userinfo_id=self.request.user.id)
        return queryset

    def list(self, request, *args, **kwargs):
        """
        重写list
        """
        response = super().list(request, *args, **kwargs)
        old_data = response.data
        new_data = common_data(old_data)
        response.data = new_data
        return response
